package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.behavior.mapper.ApBehaviorEntryMapper;
import com.heima.behavior.pojo.ApBehaviorEntry;
import com.heima.behavior.pojo.ApLikesBehavior;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.common.constants.message.ArticleBehaviorRedisPrefixConstants;
import com.heima.common.dtos.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.exception.LeadException;
import com.heima.common.threadlocal.UserThreadLocalUtils;
import com.heima.common.util.BeanHelper;
import com.heima.model.behavior.dtos.ArticleBehaviorShowDTO;
import com.heima.model.behavior.dtos.LikesBehaviorDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * <p>
 * APP行为实体表,一个行为实体可能是用户或者设备，或者其它 服务实现类
 * </p>
 *
 * @author HM
 * @since 2023-04-09
 */
@Slf4j
@Service
public class ApBehaviorEntryServiceImpl extends ServiceImpl<ApBehaviorEntryMapper, ApBehaviorEntry> implements ApBehaviorEntryService {
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public void likeBehavior(LikesBehaviorDto dto) {
        //获取当前用户id
        Integer userId = UserThreadLocalUtils.getUserId();
        if (userId == null || userId <= 0) {
            throw new LeadException(AppHttpCodeEnum.NEED_LOGIN);
        }
        String likeKey = ArticleBehaviorRedisPrefixConstants.BEHAVIOR_LIKE_PREFIX + ":" + userId;
        String unlikeKey = ArticleBehaviorRedisPrefixConstants.BEHAVIOR_UNLIKE_PREFIX + ":" + userId;

        // 判断喜欢内容类型  0文章  1动态  2评论
        if (dto.getType() == 0) {
            //文章点赞
            //1 是否点赞
            if (dto.getOperation() == 1) {
                //点赞
                redisTemplate.opsForHash().put(likeKey, dto.getArticleId().toString(), JSON.toJSONString(dto));
                //判断是否成功点赞
                Object obj = redisTemplate.opsForHash().get(likeKey, dto.getArticleId());
                if (ObjectUtils.isEmpty(obj)) {
                    ArticleBehaviorShowDTO articleBehaviorShowDTO = new ArticleBehaviorShowDTO();
                    articleBehaviorShowDTO.setIslike(true);
                    redisTemplate.opsForHash().put(likeKey, dto.getArticleId().toString(), JSON.toJSONString(articleBehaviorShowDTO));
                }
            } else
                // 0  取消点赞
                if (dto.getOperation() == 0) {
                    redisTemplate.opsForHash().put(likeKey, dto.getArticleId().toString(), JSON.toJSONString(dto));
                    //判断是否取消点赞
                    Object obj = redisTemplate.opsForHash().get(likeKey, dto.getArticleId());
                    if (ObjectUtils.isEmpty(obj)) {
                        ArticleBehaviorShowDTO articleBehaviorShowDTO = new ArticleBehaviorShowDTO();
                        articleBehaviorShowDTO.setIslike(false);
                        redisTemplate.opsForHash().put(likeKey, dto.getArticleId().toString(), JSON.toJSONString(articleBehaviorShowDTO));
                    }
                }
        } else {
            //TODO  1动态  2评论 点赞功能
            return;
        }
    }
}
